백준 15650번 N과 M

Solution

import sys

input = sys.stdin.readline

n, m = map(int, input().split())

selected = [0] * (m)


def solve(x):
    if x == m:
        for s in selected:
            print(s, end=' ')
        print()
    else:
        if x == 0:
            start = 1
        else:
            start = selected[x - 1] + 1
        for candidate in range(start, n + 1):
            selected[x] = candidate
            solve(x + 1)
            selected[x] = 0


solve(0)